<template>
	<div class="system-dept-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="deptDialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级部门">
							<el-cascader
								:options="state.parentData"
								:props="{ checkStrictly: true, value: 'id', label: 'name' }"
								placeholder="请选择上级部门"
								clearable
								class="w100"
								v-model="state.ruleForm.parentId"
							>
								<template #default="{ node, data }">
									<span>{{ data.name }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入部门名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="电话" prop="phone">
							<el-input v-model="state.ruleForm.phone" placeholder="请输入联系电话" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="邮箱">
							<el-input v-model="state.ruleForm.email" placeholder="请输入邮箱" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="state.ruleForm.sort" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-radio-group v-model="state.ruleForm.status">
								<el-radio label="0">正常</el-radio>
								<el-radio label="1">禁用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDeptDialog">
import { reactive, onMounted, ref, nextTick, getCurrentInstance } from 'vue';
import { deptApi } from '/@/api/system/dept/index';
import constConfig from '/@/utils/constConfig';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const deptDialogFormRef = ref();
const { proxy } = getCurrentInstance() as any;
const state = reactive({
	ruleForm: {} as DeptType,
	parentData: [] as RouteItems, // 上级部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	rules: {
		routePath: { required: true, message: "路由地址不能为空", trigger: "blur" },
		title: { required: true, message: "部门名称不能为空", trigger: "blur" },
		type: { required: true, message: "部门类型不能为空", trigger: "blur" },
	},
});

// 打开弹窗
const openDialog = (type: string, row?: any) => {
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	state.ruleForm = row;
	if (type === constConfig.dialogType.add) {
		state.dialog.title = '新增部门';
		state.dialog.submitTxt = '新 增';
	} else {
		state.dialog.title = '修改部门';
		state.dialog.submitTxt = '修 改';
	}
	nextTick(() => {
		deptDialogFormRef.value.clearValidate();
	});
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	deptDialogFormRef.value.validate((valid: boolean) => {
		if (valid) {
			if (state.dialog.type === constConfig.dialogType.add) {
				deptApi().save(state.ruleForm).then(() => {
					closeDialog();
					emit('refresh');
				})
			} else {
				deptApi().update(state.ruleForm).then(() => {
					closeDialog();
					emit('refresh');
				})
			}
		}
	})
};
// 页面加载时
onMounted(() => {
	deptApi().tree().then((res: any) => {
		state.parentData = proxy.handleTree(res.data ?? [], "id", "parentId");
	});
});

// 暴露变量
defineExpose({
	openDialog,
});
</script>
